/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.base.data.compare;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import work.entity.Student;
import work.entity.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Sakura Shen
 * @description
 * @date 2021/9/9
 */
public class CompareTest {

    public static void main(String[] args) {

        // Integer自带compareTo方法
        // compareTo返回结果 1,-1,0
        Integer a = 26;
        int compareRes = a.compareTo(20);
        System.out.println(compareRes);

        List<Student> personList = new ArrayList<>();
        personList.add(new Student("ace","22"));
        personList.add(new Student("xb","21"));
        personList.add(new Student("glm","36"));
        personList.add(new Student("sxy","20"));

        // 自建对象 需要实现Comparable接口的compareTo方法取比较大小
        Student ace = new Student("ace","22");
        Student xb = new Student("xb","21");
        String result = ace.compareTo(xb) == 0 ?"一样大":ace.compareTo(xb)>0?"ace大":"xb大";
        System.out.println(result);

        // 排序是需要用到比较的
        // 所以实现Comparable接口的对象是可以排序的
        // 通过Collections类反序排序
        Collections.sort(personList,Collections.reverseOrder());
        for(Student p:personList)
            System.out.println(p);


        // List的sort方法进行排序 实现Comparator接口进行排序的比较 就不需要实体类实现Comparable接口了
        // User实体类就没有实现Comparable接口接口
        List<User> userList = new ArrayList<>();
        userList.add(new User("1","ace","22"));
        userList.add(new User("2","xb","21"));
        userList.add(new User("3","glm","36"));
        userList.sort(Comparator.comparingInt(a2 -> Integer.parseInt(a2.getAge())));
        for(User p:userList)
            System.out.println(p);

        System.out.println("AA");

    }



}
